<?php
// 本类由系统自动生成，仅供测试用途
class WeekwaAction extends CommonAction {
    public function index(){
        if(isset($_SESSION['loginUserName'])){
            $this->assign('userName',$_SESSION['loginUserName']);
        }

        $curtime    = $_GET['week']?$_GET['week']:date(time());//不是表示当前时间，是当前要显示的时间
        $curYear    = date("Y",$curtime);
        $curWeek    = date("W",$curtime);

        $this->assign('preweek',$curtime-7*24*3600);
        $this->assign('nextweek',$curtime+7*24*3600);
        $this->assign('curweek',date(time()));

        //$week 		= getWeekStartAndEnd($curYear,$curWeek);
        $weekStart 	= this_monday($curtime,1);//$week['start'];
        $weekEnd    = this_sunday($curtime,1);//$week['end'];
        $_SESSION['weekStart']  = $weekStart;//主要用于在导出表格excel时使用
        $_SESSION['weekEnd']    = $weekEnd;//主要用于在导出表格excel时使用
        $wsList		= array("星期一","星期二","星期三","星期四","星期五","星期六","星期日");

        $list       = array();

        $weekwa     = M('Weekwa');
        $list       = $weekwa->where("riqi>=$weekStart and riqi <=$weekEnd")->order("riqi,sxw")->select();
    	if($list && isset($_SESSION['loginUserName'])){
            $this->assign('waFlag',1);//用于在模板显示是否使用空白表还是使用数据库数据
            $wsList1     = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六");
            //补齐一周的全部空格，包括上下午，哪一天没有就补上空格，并按日期顺序排序，为了方便排序，下午全部加了1ms
            for($i=0;$i<7;$i++){
                $t1 = $weekStart + $i*24*60*60;
                $w1 = $wsList1[date('w',$t1)];
                $c  = count($list);
                $flags = 0;
                $flagx = 0;
                //echo $i.'<br>';
                for($j=0;$j<$c;$j++){
                    //echo $t1.'  '.$list[$j]['riqi'].'<br>';
                    if($t1==$list[$j]['riqi']){
                        if('上午'==$list[$j]['sxw']){
                            $flags = 1;
                        }
                        if('下午'==$list[$j]['sxw']){
                            $flagx = 1;
                            $list[$j]['riqi'] += 1;
                        }
                    }
                }
                //emptyf主要用于隐藏空单元格的操作按钮
                if($flagx==0 && $flags==0){
                    $temp =array(array("riqi"=>$t1,"week"=>$w1,"sxw"=>"上午","emptyf"=>"emptyf"),array("riqi"=>$t1+1,"week"=>$w1,"sxw"=>"下午","emptyf"=>"emptyf"));
                    array_splice($list,0,0,$temp);
                }
                if($flagx==0 && $flags==1){
                    $temp =array(array("riqi"=>$t1+1,"week"=>$w1,"sxw"=>"下午","emptyf"=>"emptyf"));
                    array_splice($list,0,0,$temp);
                }
                if($flagx==1 && $flags==0){
                    $temp =array(array("riqi"=>$t1,"week"=>$w1,"sxw"=>"上午","emptyf"=>"emptyf"));
                    array_splice($list,0,0,$temp);
                }
            }//for
        }else{
            $this->assign('waFlag',0);
            for($i=0;$i<7;$i++){
            	$showDate 	= $weekStart + $i*24*60*60;
            	$list[$i]	= array("showWeek"=>$wsList[$i],"showDate"=>$showDate);
            }
        }
        //dump($list);
        //return;
        //$this->array_sort_by($list,'sxw');
        $this->array_sort_by($list,'riqi');

        $this->assign('list',$list);      

        //以下代码用于显示学校列表
        $School     = M('School');
        $list1      = $School->where('status = 1')->order('sortid')->field('id,sName,sType')->select();//getField('id,sName,sType',":");
        $this->assign('schoolJson',json_encode($list1)); 

        $this->display();
        return;
    }
    /**
    * 对一个多维数组的某个字段进行排序
    *
    * @param array $array 待排序数组
    * @param string $key 排序根据的字段
    * @return array 子数组的字段
    */
    function array_sort_by(&$array,$key,$sub){
        $tmp = null;
        $min = null;
        $n = count($array);
        for ($i=0;$i<=$n-1;$i++){
          $min = $i;
          if(isset($array[$i+1])){
           for($y=$i+1;$y<$n;$y++){
            if($array[$min][$key]>
            $array[$y][$key]){
             $min = $y;
            }
           }
           if($min!=$i){
            $tmp = $array[$i];
            $array[$i] = $array[$min];
            $array[$min] = $tmp;
            unset($tmp);
           }
          }
          if(isset($array[$i][$sub])) {
           $array[$i][$sub]=array_sort_by($array[$i][$sub],$key);
          }
        }
        unset($n);
        unset($min);
        return $array;
    }
}
?>